package com.example.ivideo_2211a.widget

import android.content.Context
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Shader
import android.graphics.Typeface
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.example.ivideo_2211a.R

class GoldCoinView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {

    private val coinImage = ImageView(context).apply {
        setImageResource(R.drawable.ic_gold_coin)
        layoutParams = LayoutParams(80, 80).apply {
            gravity = Gravity.CENTER
        }
        elevation = 8f
    }
    private val coinText = TextView(context).apply {
        text = "0"
        setTextColor(Color.parseColor("#FFF176"))
        textSize = 18f
        setTypeface(null, Typeface.BOLD)
        setShadowLayer(8f, 0f, 0f, Color.parseColor("#FFD600"))
        elevation = 10f
        gravity = Gravity.CENTER
    }


    // 1. 成员变量
    private var rewardCount = 0 // 已领取次数
    private val maxRewardCount = 2
    private var coinCount = 0   // 已领取金币数
    private var pendingReward = 0 // 当前待领取金币数
    // countdown 由外部传入，不再依赖 rewardInterval

    init {
        // 渐变圆形背景
        val gradientDrawable = GradientDrawable(
            GradientDrawable.Orientation.TL_BR,
            intArrayOf(Color.parseColor("#FFFDE4"), Color.parseColor("#FFD700"), Color.parseColor("#FFB300"))
        )
        gradientDrawable.shape = GradientDrawable.OVAL
        gradientDrawable.gradientType = GradientDrawable.RADIAL_GRADIENT
        gradientDrawable.gradientRadius = 120f
        gradientDrawable.setStroke(4, Color.parseColor("#FFF176"))
        background = gradientDrawable

    addView(coinImage)
    addView(coinText)
        setPadding(16, 16, 16, 16)
        isClickable = true
        isFocusable = true
        foreground = ContextCompat.getDrawable(context, android.R.drawable.list_selector_background)
    }

    // 展示View，不再包含定时器和动画逻辑

    // 3. 更新金币信息（如有需要，可直接操作coinCountdownText和coinText）
    // private fun updateGoldCoinInfo() { ... } 已废弃

    // 弹窗逻辑已移除，需由外部Fragment/Activity控制

    // 5. 生命周期和setupViews
//    override fun onStart() {
//        super.onStart()
//        handler.postDelayed(rewardRunnable, rewardInterval)
//        handler.post(countdownRunnable)
//        updateGoldCoinInfo()
//    }
//    override fun onStop() {
//        super.onStop()
//        handler.removeCallbacks(rewardRunnable)
//        handler.removeCallbacks(countdownRunnable)
//    }
//    override fun setupViews() {
//        // ...existing code...
//        updateGoldCoinInfo()
//        // ...existing code...
//    }

    fun setCoinCount(count: Int) {
        val old = coinText.text.toString()
        coinText.text = count.toString()
        if (old != count.toString()) {
            // 数字跳跃动画
            val scaleUp = ObjectAnimator.ofFloat(coinText, "scaleX", 1f, 1.3f, 1f)
            val scaleUpY = ObjectAnimator.ofFloat(coinText, "scaleY", 1f, 1.3f, 1f)
            val alpha = ObjectAnimator.ofFloat(coinText, "alpha", 1f, 0.7f, 1f)
            AnimatorSet().apply {
                playTogether(scaleUp, scaleUpY, alpha)
                duration = 350
            }.start()
        }
    }

    // setCountdown 已废弃，倒计时请在外部单独TextView展示
}
